package com.xssweibo.ui;

import java.util.Date;
import java.util.List;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xssweibo.base.NetUtil;
import com.xssweibo.base.Status;
import com.xssweibo.base.User;
import com.xssweibo.base.WeiboException;
import com.xssweibo.service.BaseService;
import com.xssweibo.service.Task;

public class Home extends Activity implements IWeiboActivity {

	public static final int VIEW_ITEM_COMMON = 0;
	public static final int VIEW_ITEM_MORE = 1;
	public static final int REFRESH_WEIBO = 1;
	public static final int REFRESH_ICON = 2;

	View process;
	ListView lv;
	public ListView allStatus;
	Button btnRef;
	Button btnNew;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		BaseService.addActivity(this);

		process = findViewById(R.id.progress);

		lv = (ListView) findViewById(R.id.freelook_listview);
		allStatus = (ListView) findViewById(R.id.freelook_listview);
		allStatus.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub

			}
		});
		allStatus.setVisibility(View.GONE);

		Task task = new Task(Task.TASK_GET_TIMELINE, null);
		BaseService.addTask(task);

		btnNew = (Button) findViewById(R.id.title_bt_left);
		btnNew.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

			}
		});

		btnRef = (Button) findViewById(R.id.title_bt_right);
		btnRef.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void refresh(Object... args) {
		switch (((Integer) args[0]).intValue()) {
		case REFRESH_WEIBO:
			try {
				process.setVisibility(View.GONE);
				List<Status> status = Status
						.constructStatuses((String) args[1]);
				MyAdapter adapter = new MyAdapter(this, status);
				lv.setAdapter(adapter);
				lv.setVisibility(View.VISIBLE);

			} catch (WeiboException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			break;

		case REFRESH_ICON:

			break;

		default:
			break;
		}

	}
}

class MyAdapter extends BaseAdapter {

	public List<Status> alls;
	public Context conts;

	public MyAdapter(Context con, List<Status> st) {
		conts = con;
		alls = st;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return alls.size() + 2;
	}

	@Override
	public int getItemViewType(int position) {
		return (position == 0 || position == (getCount() - 1)) ? Home.VIEW_ITEM_MORE
				: Home.VIEW_ITEM_COMMON;
	}
	
	@Override
	public int getViewTypeCount(){
		return Home.VIEW_ITEM_MORE+1;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int index) {
		// TODO Auto-generated method stub
		// return alls.get(index).getId();

		if (index == 0) {
			return 0;
		} else if (index > 0 && index < this.getCount() - 1) {
			return alls.get(index - 1).getId();
		} else {
			return -1;
		}
	}

	public void addMoreDate(List<Status> moreDate) {
		this.alls.addAll(moreDate);
		this.notifyDataSetChanged();
	}

	// 定义静态类用于获取处理列表中每个条目数据的更新
	private static class ViewHolder {
		ImageView ivItemPortrait;// 头像 有默认值
		TextView tvItemName;// 昵称
		ImageView ivItemV;// 新浪认证 默认gone
		TextView tvItemDate;// 时间
		ImageView ivItemPic;// 时间图片 不用修改
		TextView tvItemContent;// 内容
		ImageView contentPic;// 自己增加的内容图片显示的imgView
		View subLayout;// 回复默认gone
		TextView tvItemSubContent;// 回复内容 subLayout显示才可以显示
		ImageView subContentPic;// 自己增加的主要显示回复内容的图片。subLayout显示才可以显示
		TextView tvMore;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		int type = getItemViewType(position);
		if (convertView == null) {
			holder = new ViewHolder();
			switch (type) {
			case Home.VIEW_ITEM_COMMON:
				convertView = LayoutInflater.from(conts).inflate(
						R.layout.itemview, null);
				holder.ivItemPortrait = (ImageView) convertView
						.findViewById(R.id.ivItemPortrait);
				holder.tvItemName = (TextView) convertView
						.findViewById(R.id.tvItemName);
				holder.tvItemContent = (TextView) convertView
						.findViewById(R.id.tvItemContent);
				holder.tvItemDate = (TextView) convertView
						.findViewById(R.id.tvItemDate);
				break;

			case Home.VIEW_ITEM_MORE:
				convertView = LayoutInflater.from(conts).inflate(
						R.layout.list_moreitems, null);
				holder.tvMore = (TextView) convertView
						.findViewById(R.id.textView);
				break;

			}

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Log.d("listview", "listv:" + position);
		
		switch (type) {
		case Home.VIEW_ITEM_COMMON:
			// 设定昵称
			User user = alls.get(position - 1).getUser();
			if (user != null) {
				holder.tvItemName.setText(alls.get(position - 1).getUser()
						.getScreenName());

				// 获取头像
				if (BaseService.arrayUserIcon.get(user.getId()) != null) {
					holder.ivItemPortrait
							.setImageDrawable(BaseService.arrayUserIcon
									.get(user.getId()));
				}
			}

			// 设定内容
			holder.tvItemContent.setText(alls.get(position - 1).getText());

			Date date = null;
			try {
				date = NetUtil.parseDate(alls.get(position - 1)
						.getCreatedAt(), "EEE MMM dd HH:mm:ss z yyyy");
			} catch (WeiboException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			holder.tvItemDate.setText(NetUtil.getTimeDiff(date));
			break;

		case Home.VIEW_ITEM_MORE:
			holder.tvMore.setText("刷新");
			break;

		}

		return convertView;
	}
}
